using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Template : BaseItem
    {
        private Template() : base() { }
        private Template(Hashtable table) : base(table) { }
        private Template(Hashtable table, List<Template> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TemplateDal();
        }

        public static Template CreateInstance()
        {
            return new Template();
        }

        private static List<Template>  GetItems(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<Template> tmp = new List<Template>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Template(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static List<Template> GetAllBySiteId(int siteId)
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAllBySiteId(siteId));
        }

        public static List<Template> GetAll()
        {
            TemplateDal dal = new TemplateDal();
            return GetItems(dal.GetAll());
        }

        public static Template GetById(int id)
        {
            TemplateDal dal = new TemplateDal();
            Hashtable table = dal.GetById(id);
            if (table != null)
            {
                return new Template(table);
            }
            else
            {
                return null;
            }
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[TemplateDal.Fields.Id];
            siteId = (int)table[TemplateDal.Fields.SiteId];
            name = (string)table[TemplateDal.Fields.Name];

        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int siteId;
        public int SiteId
        {
            get { return siteId; }
            set { siteId = value; }
        }


        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public new void Update()
        {
            base.Update(); 
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((TemplateDal)dal).Update(ref id, siteId, name, ref concurencyId);
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateDal)dal).Delete(id, concurencyId);
        }
    }
}
